<template>
  <view class="query_container">
    <!-- 开启霸王餐的活动 -->
    <view class="bargin_container"> </view>
    <!-- 开启霸王餐的活动 -->
    <!-- 商品展示区域 -->
    <view class="list" v-if="!rule_show">
      <scroll-view
        :scroll-y="true"
        class="scroll-y"
        @scrolltolower="scroll_bottom"
      >
        <bargin-good
          :goodsList="goodList"
          v-if="goodList.length > 0"
          @goods_down="goods_down"
          @edit="edit(arguments)"
          @del_goods="del_goods"
          :timestamp="timestamp"
          type="near"
        ></bargin-good>
      </scroll-view>
      <!-- 商品展示区域 -->
      <view class="empty" style="text-align: center" v-if="goodList.length == 0"
        >暂无商品,去添加</view
      >
      <view class="toAssistant1" @tap="go_add_goods"> 添加商品 </view>
    </view>

    <van-dialog id="van-dialog" />
    <van-toast id="van-toast" />
  </view>
</template>

<script>
import BarginGood from "../near_mch_bargin/components/can_bargin_goods";
import uImg from "../../components/zhtx-uploadImg/zhtx-uploadImg";
import {
  cutprice_list,
  show_cp_goods,
  query_xp_goods,
  update_near_goods,
  cutprice_statusupdate,
} from "../../utils/imapi";
import Dialog from "../../wxcomponents/vant/dialog/dialog";
export default {
  provide(){
    return{
      reload:this.reload
    }
  },
  props: {},
  components: {
    BarginGood,
    uImg,
  },
  data() {
    return {
      value: "",
      option1: [
        { text: "全部", value: 0 },
        { text: "待付款", value: 1 },
        { text: "待发货", value: 2 },
        { text: "交易完成", value: 3 },
        { text: "退款退货中", value: 4 },
        { text: "已退款", value: 5 },
        { text: "交易取消", value: 6 },
      ],
      goodList: [],
      value1: 0,
      queryparam: {
        page_index: 1,
        page_size: 10,
        status: "",
        create_time_start: "",
        create_time_end: "",
        cut_price: "",
        cut_mode: "3",
      },
      bargin_ruleForm: {},
      rule_show: false,
      uImgList: [],
      value: "",
      uploadFileUrl:
        "https://api.comm.t.xingkonglian.net:8098/api/v1/oss/upload_starx",
      limit: 1,
      name: "file",
      show_time_picker: false,
      minHour: "00",
      maxHour: "23",
      countdown: "",
      editIndex: 0,
      timestamp: "",
      need_refresh: true,
    };
  },
  computed: {},
  watch: {},
  onLoad() {
    this.cutprice_list();
  },
  onReady() {},
  onShow() {
    // if(this.need_refresh) {
    //   this.need_refresh=false
    //   this.cutprice_list();
    // }
    // this.cutprice_list();
  },
  methods: {
    reload() {
      this.cutprice_list()
    },
    async cutprice_list() {
      const timestamp = Date.parse(new Date()).toString();
      console.log(timestamp);
      this.timestamp = timestamp;
      try {
        const data = await cutprice_list(this.queryparam);
        console.log(data);
        if (data.code == 0) {
          this.goodList = data.data;
          console.log(this.goodList);
        }
        return data;
      } catch (error) {}
    },
    status_change(e) {
      console.log(e.detail);
    },
    inputchange(e) {
      console.log(e.detail);
      this.queryparam.title = e.detail.value;
    },
    reset() {
      this.queryparam = {
        goods_status: "",
        page_index: 1,
        page_size: 10,
        title: "",
        load_spec: "1",
      };
      this.query_all_goods();
    },
    thequery() {
      console.log(this.queryparam.title);
      this.query_all_goods();
    },
    async scroll_bottom() {},
    go_add_goods() {
      uni.navigateTo({
        url: "/pages/mch_goods/mch_goods?from=near&goods_status=1",
      });
    },
    goods_down(e, index) {
      console.log(e, index);
      let title = "";
      title = e.status == 1 ? "下架" : "上架";
      Dialog.confirm({
        title: "确定" + title + "此砍价商品?",
      })
        .then(async () => {
          // on confirm
          const data = await update_near_goods({
            id: e.id,
            status: e.status == 1 ? "0" : "1",
          });
          if (data.code == 0) {
            this.$toast.success("操作成功");
            console.log(this.goodList[index].status);
            if (e.status == 1) {
              this.goodList[index].status = 0;
            } else {
              console.log("222");
              this.goodList[index].status = 1;
            }
            // this.$forceUpdate();
            // this.cutprice_list();
          }
        })
        .catch(() => {
          // on cancel
        });
    },
    edit(e) {
      console.log(e);
      if (e.status == "1") {
        return this.$toast("请先下架此商品再进行编辑操作");
      }
      const item = JSON.stringify(e[0]);
      uni.navigateTo({
        url: "/pages/edit_bargin/edit_bargin?item=" + item + "&index=" + e[1],
      });
      // this.editIndex = index
      // this.bargin_ruleForm = e;
      // this.uImgList = [];
      // this.uImgList.push(e.pos_pic);
      // this.rule_show = true;
    },
    del_goods(e, index) {
      Dialog.confirm({
        title: "确定删除此砍价商品?",
      })
          .then(async () => {
            // on confirm
            const data = await cutprice_statusupdate({
              id: e.id,
              status: "2",
            });
            if (data.code == 0) {
              this.$toast.success("操作成功");
              this.reload();
            }
          })
          .catch(() => {
            // on cancel
          });
    },
    change_need_times(e) {
      this.bargin_ruleForm.need_times = e.detail;
    },
    change_per_times(e) {
      this.bargin_ruleForm.one_people_times = e.detail;
    },
    change_cut_title(e) {
      this.bargin_ruleForm.cut_item_title = e.detail;
    },
    change_cut_price(e) {
      this.bargin_ruleForm.cut_price = e.detail;
    },
    change_suc_times(e) {
      this.bargin_ruleForm.success_times = e.detail;
    },
    change_open_times(e) {
      this.bargin_ruleForm.open_times = e.detail;
    },
    change_show_need_times(e) {
      console.log(e);
      this.bargin_ruleForm.show_need_times = e.detail ? 1 : 0;
    },
    change_show_bj_price(e) {
      console.log(e);
      this.bargin_ruleForm.show_bj_price = e.detail ? 1 : 0;
    },
    uploadSuccess(result) {
      if (result.statusCode == 200) {
        //上传成功的回调处理
        console.log(result.data);
        const img = JSON.parse(result.data).data;
        console.log(img);
        this.bargin_ruleForm.pos_pic = img;
        console.log(result);
      } else {
      }
    },
    async submit() {
      console.log(this.bargin_ruleForm);
      if (
        this.bargin_ruleForm.need_times == "" ||
        this.bargin_ruleForm.one_people_times == "" ||
        this.bargin_ruleForm.cut_item_title == "" ||
        this.bargin_ruleForm.pos_pic == "" ||
        this.bargin_ruleForm.price == "" ||
        (this.bargin_ruleForm.open_times == "" &&
          this.bargin_ruleForm.open_times != 0) ||
        (this.bargin_ruleForm.success_times == "" &&
          this.bargin_ruleForm.success_times != 0)
      ) {
        this.$toast("请填写完整数据");
        return;
      }
      this.bargin_ruleForm.hour = Number(this.bargin_ruleForm.hour);
      this.bargin_ruleForm.minutes = Number(this.bargin_ruleForm.minutes);
      this.bargin_ruleForm.need_times = Number(this.bargin_ruleForm.need_times);
      this.bargin_ruleForm.one_people_times = Number(
        this.bargin_ruleForm.one_people_times
      );
      this.bargin_ruleForm.open_times = Number(this.bargin_ruleForm.open_times);
      this.bargin_ruleForm.price = Number(this.bargin_ruleForm.price);
      this.bargin_ruleForm.success_times = Number(
        this.bargin_ruleForm.success_times
      );
      this.bargin_ruleForm.show_need_times = Number(
        this.bargin_ruleForm.show_need_times
      );
      this.bargin_ruleForm.show_bj_price = Number(
        this.bargin_ruleForm.show_bj_price
      );
      this.bargin_ruleForm.limit_radius_pub = Number(
        this.bargin_ruleForm.limit_radius_pub
      );
      try {
        const data = await item_save(this.bargin_ruleForm);
        if (data.code == 0) {
          this.$toast.success("编辑成功");
          if (this.goodList[this.editIndex] == undefined) {
            this.goodList[this.editIndex] = {};
          }
          this.goodList[this.editIndex].pos_pic = this.bargin_ruleForm.pos_pic;
          this.goodList[
            this.editIndex
          ].cut_item_title = this.bargin_ruleForm.cut_item_title;
          this.rule_show = false;
          // this.cutprice_list();
        }
      } catch (err) {
        console.log(err);
        this.$toast.success("编辑失败");
      }
    },
    cancle_submit() {
      console.log("111");
      this.rule_show = false;
    },
    upload() {
      this.$refs.upimg.upload();
    },

  },
  async onReachBottom() {
    console.log("触底了");
    this.queryparam.page_index++;
    try {
      const data = await cutprice_list(this.queryparam);
      // const toast = this.$toast.loading("加载中");
      if (data.code == 0) {
        this.goodList.push(...data.data);
        this.$toast.clear();
        console.log(data);
        if (data.data.length == 0) {
          this.queryparam.page_index--;
          this.$toast("全部加载");
        }
      }
    } catch (error) {}
  },

  onHide() {},
};
</script>

<style scoped lang="scss" scoped>
.query_container {
  .query_box {
    width: 750upx;
    height: 220upx;
    background: #f0eefd;
    padding: 32upx;
    /deep/ .van-cell {
      height: 68upx;
      background: #ffffff;
      border-radius: 38upx;
      display: flex;
      align-items: center;
      .input {
        height: 100%;
        font-size: 24upx;
      }
    }
    .query_btn_box {
      margin-top: 20upx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      /deep/ .van-dropdown-menu {
        width: 320upx;
        height: 68upx;
        background: #ffffff;
        border-radius: 34upx;
      }
      /deep/ .reset_btn {
        width: 320upx;
        height: 68upx;
        background: #ffffff;
        border-radius: 34upx;
        border: 2upx solid #6d55ed;
        text-align: center;
        line-height: 68upx;
        font-size: 28upx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #6d55ed;
      }
      /deep/ .query_btn {
        width: 320upx;
        height: 68upx;
        background: #6d55ed;
        border-radius: 34upx;
        text-align: center;
        line-height: 68upx;
        font-size: 28upx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #fff;
      }
    }
  }
}
.toAssistant1 {
  position: fixed;
  bottom: 10upx;
  width: 690upx;
  left: 30upx;
  /* right: 0; */
  height: 80upx;
  background: #f03752;
  border-radius: 50upx;
  text-align: center;
  line-height: 80upx;
  font-size: 32upx;
  color: #fff;
  font-weight: 600;
  font-family: PingFangSC-Semibold, PingFang SC;
  z-index: 99;
  /* margin-top: 60upx; */
}
.btn_box {
  padding: 0 20px;
  margin-top: 20upx;
  /deep/ .van-button:first-child {
    margin-bottom: 20upx;
  }
}
.upload {
  display: flex;
  align-items: center;
}
</style>
